/**
 * Created by Administrator on 2017/7/12 0012.
 */

/*
 页面加载参数
 */
var ulid="#mainbody";
//$.cookie("MYID","81148e278cfae5e5");
if($.cookie("MYID")){var id= $.cookie("MYID");}
//id="81148e278cfae5e5";
var pageSize=15;//默认一页加载15条数据
var currentIndex=1;//页码
if(!$.cookie("pulutype")){$.cookie("pulutype",1)};//第一次进入没有类型
if(!$.cookie("pululot")){$.cookie("pululot","")};//第一次进入
if(!$.cookie("pululat")){$.cookie("pululat","")};//第一次进入
/*
 页面跳转
 */
//联系人
$("#gofirend").click(function(){
    if($.cookie("MYID")){
        location.href="http://www.pulu.com/weixin/chatpage.html"
    }
});
//订阅
$("#gosubsc").click(function(){
    if($.cookie("MYID")){
        location.href="http://www.pulu.com/weixin/subscribe.html"
    }
});
//我
$("#gomy").click(function(){
    if($.cookie("MYID")){
        location.href="http://www.pulu.com/weixin/setpage.html"
    }
});
/*
* 高德地图
*/
function mapftn(){
    //没有坐标信息
    if(!$.cookie("pululot")||!/[0-9]/.test($.cookie("pululot"))){
        var map, geolocation;
        //加载地图，调用浏览器定位服务
        map = new AMap.Map('mapplace', {
            resizeEnable: true
        });
        map.plugin('AMap.Geolocation', function() {
            geolocation = new AMap.Geolocation({
                enableHighAccuracy: true,//是否使用高精度定位，默认:true
                noIpLocate:0,
                GeoLocationFirst:true,//默认为false，设置为true的时候可以调整PC端为优先使用浏览器定位，失败后使用IP定位
                convert: true,         //自动偏移坐标，偏移后的坐标为高德坐标，默认：true
                timeout: 15000          //超过15秒后停止定位，默认：无穷大
            });
            map.addControl(geolocation);
            geolocation.getCurrentPosition();
            AMap.event.addListener(geolocation, 'complete', onComplete);//返回定位信息
            AMap.event.addListener(geolocation, 'error', onError);      //返回定位出错信息
        });
        //解析定位结果
        function onComplete(data) {
            var expiresDate= new Date();
            expiresDate.setTime(expiresDate.getTime() + (60 * 60 * 1000));
            //有效60分钟
            $.cookie("pululot",data.position.lng,{expires : expiresDate});
            $.cookie("pululat",data.position.lat,{expires : expiresDate});
            getallnote(currentIndex,pageSize);
        }
        //解析定位错误信息
        function onError(data) {
            $("#nonews").css("display","block");
            $("#waiting1").css("display","none");
            $("#nonews1").css("display","none");
        }
    }
    //保存坐标信息
    else{
        getallnote(currentIndex,pageSize);
    }
}

/*
* 保存跳转页码 距离
*/
function savepage(){
    var scrollTop=$(window).scrollTop();
    $.cookie("puluscrollTop",scrollTop);
    $.cookie("pulucurrentIndex",currentIndex);
    //alert($.cookie("pulucurrentIndex"));
}

/*
 判断是否在微信打开
*/
function is_weixn(){
    var ua = navigator.userAgent.toLowerCase();
    if(ua.match(/MicroMessenger/i)=="micromessenger") {return true;} else {return false;}
}

/*
 展示加载图片
*/
function showloadimg(e){
    $(e).fadeIn();
}

/*
* 时间 几天前 几分钟前
*/
function getDateTimeStamp(dateStr){
    dateStr=dateStr.match(/[0-9\-T\:]+/)[0].replace(/T/g,' ');
    return Date.parse(dateStr.replace(/-/gi,"/"));
}
function getDateDiff(dateTimeStamp){
    var minute = 1000 * 60;
    var hour = minute * 60;
    var day = hour * 24;
    var halfamonth = day * 15;
    var month = day * 30;
    var now = new Date().getTime();
    var diffValue = now - dateTimeStamp;
    if(diffValue < 0){ result="刚刚";return;}
    var monthC =diffValue/month;
    var weekC =diffValue/(7*day);
    var dayC =diffValue/day;
    var hourC =diffValue/hour;
    var minC =diffValue/minute;
    if(monthC>=1){result="" + parseInt(monthC) + "月前";}
    else if(weekC>=1){result="" + parseInt(weekC) + "周前";}
    else if(dayC>=1){result=""+ parseInt(dayC) +"天前";}
    else if(hourC>=1){result=""+ parseInt(hourC) +"小时前";}
    else if(minC>=1){result=""+ parseInt(minC) +"分钟前";}
    else result="刚刚";
    return result;
}

/*
 消息通知
 */
if($.cookie("MYID")){
    $.ajax({
        type:"get",
        url:getanswernumurl+id,
        datatype:"json",
        success:function(d){
            if(d.Data>0){
                $("#newnotenotice").css("display","block");
                $("#newnotenotice").html(d.Data);
            }
        }
    });
    //申请通知
    $.ajax({
        type:"get",
        url:getfriendnotenumurl+id,
        datatype:"json",
        success:function(d){
            if(d.Data>0){
                $("#gofirend i").css("display","block");
            }
        }
    });
    //留言通知
    $.ajax({
        type:"get",
        url:messagenotenumurl+id,
        datatype:"json",
        success:function(d){
            //console.log(d);
            if(d.Data.msg=="有新留言"){
                $("#gofirend i").css("display","block");
            }
        }
    });
}

/*
 搜索
*/
$("#gotosearch").click(function(){
    if($.cookie("MYID")){
        location.href="http://www.pulu.com/weixin/searchdetail.html";
    }
});

/*
 发经验问题
*/
$("#sendbtn").click(function(){
    var loca=location.href;
    if(is_weixn()){
        $.cookie("expkeyword","");
        $.cookie("expType","");
        $.cookie("sendque","");
        /questionchikui/.test(loca)?$.cookie("expType","chikui"):/questionhuasuan/.test(loca)?$.cookie("expType","huasuan"):/questionxinqi/.test(loca)?$.cookie("expType","xinqi"):/questiondianying/.test(loca)?$.cookie("expType","tuijiandy"):/questionfandian/.test(loca)?$.cookie("expType","tuijianfd"):/questionyouwan/.test(loca)?$.cookie("expType","youwan"):/questionshu/.test(loca)?$.cookie("expType","shu"):/questionqita/.test(loca)?$.cookie("expType","qita"):/questionjk/.test(loca)?$.cookie("expType","jkjy"):/questiongw/.test(loca)?$.cookie("expType","gwjy"):/questiongz/.test(loca)?$.cookie("expType","gzjy"):/questionye/.test(loca)?$.cookie("expType","yejy"):$.cookie("expType","");
        location.href="http://www.pulu.com/weixin/sendexpgpc.html";
    }
    else{
        eid="";
        var expType="";
        /questionchikui/.test(loca)?expType="&expType=chikui":/questionhuasuan/.test(loca)?expType="&expType=huasuan":/questionxinqi/.test(loca)?expType="&expType=xinqi":/questiondianying/.test(loca)?expType="&expType=tuijiandy":/questionfandian/.test(loca)?expType="&expType=tuijianfd":/questionyouwan/.test(loca)?expType="&expType=youwan":/questionshu/.test(loca)?expType="&expType=shu":/questionqita/.test(loca)?expType="&expType=qita":/questionye/.test(loca)?expType="&expType=yejy":/questiongz/.test(loca)?expType="&expType=gzjy":/questiongw/.test(loca)?expType="&expType=gwjy":/questionjk/.test(loca)?expType="&expType=jkjy":expType="";
        location.href="https://www.pulu.com/weixin/sendexpgpc.html?id="+id+eid+expType;
    }
});

/*
 点击用户头像
 */
function usergohref(e){
    if($.cookie("MYID")){
        location.href="http://www.pulu.com/weixin/otherpeoplepage.html?otherid="+e.dataset.uid;
    }
}

/*
经验加载方法
*/
    //跳转方法
    function hrefgo(d){
        var url= d.dataset.href;
        savepage();//保存页码 距离
        //清除转发者信息 防止返回信息保存
        $.cookie("shareUserId","");
        $.cookie("fromuser","");
        if(d.dataset.shareuserid){
            //shareuser="&shareuser="+e.dataset.shareuserid;
            $.cookie("shareUserId",d.dataset.shareuserid);
        }
        location.href=url;
    }
    //发高额
    function gotoheiexp(data){
        var eid=data.dataset.keyword;
        var e=arguments.callee.caller.arguments[0]||event;
        var loca=location.href;
        if(e&&e.stopPropagation()){e.stopPropagation();}else if(window.event){window.event.cancelBubble = true;}
        savepage();//保存页码 距离
        if(data.dataset.qtype==2){
            if(is_weixn()){
                $.cookie("expkeyword",eid);
                $.cookie("sendque","question");
                location.href="http://www.pulu.com/weixin/sendexpgpc.html";
            }
            else{
                location.href="https://www.pulu.com/weixin/sendexpgpc.html?id="+id+"&keyword="+eid+"&sendque=question";
            }
        }
        else{
            if(is_weixn()){
                $.cookie("expkeyword",eid);
                $.cookie("expType","");
                $.cookie("sendque","");
                /questionchikui/.test(loca)?$.cookie("expType","chikui"):/questionhuasuan/.test(loca)?$.cookie("expType","huasuan"):/questionxinqi/.test(loca)?$.cookie("expType","xinqi"):/questiondianying/.test(loca)?$.cookie("expType","tuijiandy"):/questionfandian/.test(loca)?$.cookie("expType","tuijianfd"):/questionyouwan/.test(loca)?$.cookie("expType","youwan"):/questionshu/.test(loca)?$.cookie("expType","shu"):/questionqita/.test(loca)?$.cookie("expType","qita"):/questionjk/.test(loca)?$.cookie("expType","jkjy"):/questiongw/.test(loca)?$.cookie("expType","gwjy"):/questiongz/.test(loca)?$.cookie("expType","gzjy"):/questionye/.test(loca)?$.cookie("expType","yejy"):$.cookie("expType","");
                location.href="http://www.pulu.com/weixin/sendexpgpc.html";
            }
            else{
                var expType="";
                /questionchikui/.test(loca)?expType="&expType=chikui":/questionhuasuan/.test(loca)?expType="&expType=huasuan":/questionxinqi/.test(loca)?expType="&expType=xinqi":/questiondianying/.test(loca)?expType="&expType=tuijiandy":/questionfandian/.test(loca)?expType="&expType=tuijianfd":/questionyouwan/.test(loca)?expType="&expType=youwan":/questionshu/.test(loca)?expType="&expType=shu":/questionqita/.test(loca)?expType="&expType=qita":/questionye/.test(loca)?expType="&expType=yejy":/questiongz/.test(loca)?expType="&expType=gzjy":/questiongw/.test(loca)?expType="&expType=gwjy":/questionjk/.test(loca)?expType="&expType=jkjy":expType="";
                location.href="https://www.pulu.com/weixin/sendexpgpc.html?id="+id+"&keyword="+eid+expType;
            }
        }
    }
    //跳转经验类别
    function goexptype(d){
        //取消事件冒泡
        var e=arguments.callee.caller.arguments[0]||event; //若省略此句，下面的e改为event，IE运行可以，但是其他浏览器就不兼容
        if (e && e.stopPropagation) {
            // this code is for Mozilla and Opera
            e.stopPropagation();
        } else if (window.event) {
            // this code is for IE
            window.event.cancelBubble = true;
        }
        savepage();//保存页码 距离
        switch(parseInt(d.dataset.type)){
            case 1:location.href="http://www.pulu.com/weixin/questionlistjy.html?type=1";break;//吃亏
            case 2:location.href="http://www.pulu.com/weixin/questionlistjy.html?type=2";break;//划算
            case 3:location.href="http://www.pulu.com/weixin/questionlistjy.html?type=3";break;//新奇
            case 4:location.href="http://www.pulu.com/weixin/questionlistjy.html?type=4";break;//其他
            case 5:location.href="http://www.pulu.com/weixin/questionlistjy.html?type=5";break;//电影
            case 6:location.href="http://www.pulu.com/weixin/questionlistjy.html?type=6";break;//美食
            case 7:location.href="http://www.pulu.com/weixin/questionlistjy.html?type=7";break;//旅游
            case 8:location.href="http://www.pulu.com/weixin/questionlistjy.html?type=8";break;//读书
            case 22:
                if(!is_weixn){location.href="https://www.pulu.com/weixin/questionlistjy.html?type=22&id="+$.cookie("MYID")}
                else{location.href="http://www.pulu.com/weixin/questionlistjy.html?type=22"};
                break;//附近
            case 23:location.href="http://www.pulu.com/weixin/questionlistjy.html?type=23";break;//人生
            case 24:location.href="http://www.pulu.com/weixin/questionlistjy.html?type=24";break;//育儿
            case 25:location.href="http://www.pulu.com/weixin/questionlistjy.html?type=25";break;//工作
            case 26:location.href="http://www.pulu.com/weixin/questionlistjy.html?type=26";break;//健康
            case 27:location.href="http://www.pulu.com/weixin/questionlistjy.html?type=27";break;//购物
        }
    }
    //经验
    function jy(d){
        //全部经验页 返回家在重复使用jyhaveajax进行判断 pulupagejy.js
        if(d.title&& d.title.length>0){
            //匿名
            var unick="";
            if(d.questionUserNick){
                if(d.ishidename==1){unick="<a>匿名</a>";}else{unick="<a href='http://www.pulu.com/weixin/otherpeoplepage.html?otherid="+d.questionUserId+"'>"+d.nick+"</a>";}
            }
            //头像
            var uimgword="";
            if(d.questionUserAva){
                if(d.ishidename==1){
                    uimgword="<a href='javascript: ;'><img class='profile_photo_img' src='../img/af8.png' onload='showloadimg(this)'></a>";
                }else{
                    uimgword="<a href='otherpeoplepage.html?otherid="+d.questionUserId+"'><img class='profile_photo_img' src='http://42.62.66.198:8003/"+d.questionUserAva+"' onload='showloadimg(this)' style='display:none;'></a>";
                }
            }else{
                if(d.ishidename==1){
                    uimgword="<a href='javascript: ;'><img class='profile_photo_img' src='../img/af8.png' onload='showloadimg(this)' style='display:none;'></a>";
                }else{
                    uimgword="<a href='otherpeoplepage.html?otherid="+d.questionUserId+"'><img onclick='gohrefftn(this)' class='profile_photo_img' src='../img/af8.png' onload='showloadimg(this)' style='display:none;'></a>";
                }
            };
            var otherid=d.userId;
            //时间
            var tim="";
            if(d.shareTime){
                tim=getDateDiff(getDateTimeStamp(d.shareTime));
            }
            //有没有图片
            var haveimg="";
            if(d.imgcount>0){
                haveimg="<span class='isheighlogo haveimg'></span>"
            }
            //是不是推荐
            var tuijianword="";
            var moneysty='';
            if(d.isstanger===1){
                tuijianword="系统推荐 ";
                //tuijianword="<div class='tuijian'></div>";
                //moneysty='style="position:absolute;right:64px;"';
            }
            //有没有钱
            var money="";
            if(d.score>0){
                var score= parseFloat(d.score)/100;
                money="<span class='havemy jy' "+moneysty+">¥ "+score+"</span>";
            }
            //是不是转换
            var isconvert="";
            if(d.isconvert&&d.isconvert===1){
                isconvert="(悬赏完成转经验)"
            }
            //我也说说
            var gotoheiexp="<div class='gotoheiexp' onclick='gotoheiexp(this)' data-keyword="+d.qid+">这些关键词,我也来说说</div>";
            //高额
            var isheilogo="";
            if(d.isHighExp===1||d.isHighExp===2){
                isheilogo="<span class='isheighlogo'></span>";
            }
            //经验类型
            var exptypeword="";
            if(d.expTypeInfo){
                for(var i=0;i<d.expTypeInfo.length;i++){
                    exptypeword+='<span class="exptype" onclick="goexptype(this)" data-type="'+d.expTypeInfo[i].id+'">'+d.expTypeInfo[i].name+'</span>·';
                }
            }
            //标题
            //var tit=codetoface(d.title)+"(还有"+d.wordcount+"字)";
            var tit=codetoface(d.title);
            var count=0;
            if(d.count){count=d.count;}
            //昵称
            var unick="";
            if(d.ishidename===1){
                unick="<a>"+tuijianword+"匿名</a>";
            }else{
                unick="<a href='otherpeoplepage.html?otherid="+d.questionUserId+"'>"+tuijianword+"<i class='usernick'> "+d.questionUserNick+"</i></a>";
            }
            //位置
            var location="";
            if(d.pululocation){
                location="<div style='position:relative;'><img src='img/dingwei.png' style='height:12px;position: absolute;bottom:2px;left:0;'/><span style='color:#999;padding-left:15px;font-size:12px;'>"+d.pululocation+"</span></div>"
            }
            //关键词
            var keywordnor;
            var keyword="<span class='keywordtit'>关于：</span>";
            if(d.keywords){
                keywordnor=d.keywords.split(",");
                for(var a=0;a<keywordnor.length;a++){
                    keyword+="<span>"+keywordnor[a]+"</span>";
                }
            }
            var fromnick="经验";//来自
            var des="";
            //不是转发
            if(!d.shareUserId){
                if(!tim){tim="刚刚";}
            }
            //转发
            else if(d.shareUserId&&d.shareUserId.length>0){
                if(d.des){des=codetoface(d.des);}
                var count=0;
                if(d.count){count=d.count;}
                var sharetim=getDateDiff(getDateTimeStamp(d.shareTime.match(/[0-9\-T\:]+/)[0]));
                //var tim=getDateDiff(getDateTimeStamp(d.ctime.match(/[0-9\-T\:]+/)[0]));
                unick="<a href='otherpeoplepage.html?otherid="+d.shareUserId+"'><i class='usernick'>"+d.shareUserNick+"</i> 的转发</a>";
                if(d.shareUserAva){
                    uimgword="<a href='otherpeoplepage.html?otherid="+d.shareUserId+"'><img class='profile_photo_img' src='http://42.62.66.198:8003/"+d.shareUserAva+"' onload='showloadimg(this)' style='display:none;'></a>";
                }else{
                    uimgword="<a href='otherpeoplepage.html?otherid="+d.shareUserId+"'><img onclick='gohrefftn(this)' class='profile_photo_img' src='../img/af8.png' onload='showloadimg(this)' style='display:none;'></a>";
                };
                var location="";
                if(d.pululocation){
                    location="<div style='position:relative;margin-bottom:6px;'><img src='img/dingwei.png' style='height:12px;position: absolute;bottom:2px;left:0;'/><span style='color:#999;padding-left:15px;font-size:12px;'>"+d.pululocation+"</span></div>"
                }
                //转发调整金额样式
                var moneysty='';
                money="<span class='havemy jy' "+moneysty+">¥ "+score+"</span>";
                if(!sharetim){sharetim="刚刚";}
                tim=sharetim;
                var  peopleurl="'otherpeoplepage.html?otherid="+d.questionUserId+"'";
            };
            $("#mainbody .addlistplace").append("<div class='pagedlist_item shareexperience' data-money='"+d.score+"' onclick='hrefgo(this)' data-page='#mainbody' data-euser='"+d.questionUserId+"' data-href='http://www.pulu.com/weixin/shareexppage.html?eid="+d.qid+"#fromuser="+id+"' data-shareuserid='"+d.shareUserId+"'><div class='feed_item feed_type_question'><div class='borderbox'>"+"<div class='ContentHeader'><div class='photo_info_layout size_small'><div class='photo_wrapper'><div class='photo'>"+uimgword+"</div></div><div class='info_wrapper'><span class='feed_item_answer_user'> <span class='user'>"+unick+"</span>"+money+"</span><div class='ContentFooter'><span style='color:#598abf;'>"+exptypeword+"</span>"+tim+"</div></div></div></div>"+"<div class='info_wrapper' style='margin-bottom:7px;'> <div class='info'> <span class='feed_item_answer_user'>"+des+"</span> </div> </div> <h2><a class='question_link'>"+isconvert+tit+"("+d.WordCount+"字)"+haveimg+isheilogo+"</a></h2><div class='ActionBar Question'>"+location+"<div class='question_topics_container' onclick='gotoheiexp(this)' data-keyword="+d.qid+"><div class='QuestionTopics has_topics'>"+keyword+gotoheiexp+"</div></div><div class='action_bar_inner'><div class='action_item'> <a class='ReadMoreActionItem'>"+count+" 浏览 · "+ d.payCount+" 付费 · "+ d.webchatCount+" 转发到微信</a></div></div></div>"+"</div></div></div>")
        };
    };

/*
 问题加载方法
 */
    //拿问题数据
    function que(d){
        if(d.title&&d.title.length>0){
            var typename="悬赏";
            //匿名
            var unick="";
            if(d.questionUserNick){
                if(d.ishidename==1){
                    unick="<a>匿名</a>";
                }else{
                    unick="<a href='otherpeoplepage.html?otherid="+d.questionUserId+"'>"+d.nick+"</a>";
                }
            }
            //头像
            var uimgword="";
            if(d.questionUserAva&&d.ishidename!=1){
                if(d.ishidename==1){
                    uimgword="<a><img onclick='gohrefftn(this)' class='profile_photo_img' src='../img/af8.png' onload='showloadimg(this)' style='display:none;'></a>";
                }else{
                    uimgword="<a href='otherpeoplepage.html?otherid="+d.questionUserId+"'><img class='profile_photo_img' src='http://42.62.66.198:8003/"+d.questionUserAva+"' onload='showloadimg(this)' style='display:none;'></a>";
                }
            }else{
                if(d.ishidename==1){
                    uimgword="<a><img class='profile_photo_img' src='../img/af8.png' onload='showloadimg(this)' style='display:none;'></a>";
                }else{
                    uimgword="<a href='otherpeoplepage.html?otherid="+d.questionUserId+"'><img onclick='gohrefftn(this)' class='profile_photo_img' src='../img/af8.png' onload='showloadimg(this)' style='display:none;'></a>";
                }
            };
            //有没有图片
            var haveimg="";
            var showimgul="";
            if(d.qImgs){
                haveimg="<span class='isheighlogo haveimg'></span>";
                for(var i=0;i< d.qImgs.length;i++){
                    showimgul+='<li class="showimgli">'+
                    '<img src="'+imgurl+d.qImgs[i]+'">'+
                    '</li>'
                }
                showimgul='<ul class="showimgul" style="overflow: hidden;width: 15.5rem;margin-top: .125rem;">'+
                showimgul+
                '</ul>';
            }
            var money="";//有没有钱
            if(d.score>0){
                var score= parseFloat(d.score)/100;
                money="<span class='havemy xs'> 悬赏 ¥ "+score+"</span>";
            }
            var tim="";
            if(d.shareTime){tim=getDateDiff(getDateTimeStamp(d.shareTime));}
            var count=0;
            if(d.count){count=d.count;}
            //结帖
            var answerword="";
            var overword="";
            if(d.status.toString()=="0"){
                overword="(已结帖)";
                money="<span class='havemy xs'> ¥ "+score+"已分配</span>";
            }
            //问题转经验  回答数 付费数
            var tohrefurl="http://www.pulu.com/weixin/questiondetail.html?qid="+d.qid+"#fromuser="+id;
            var tranword="";
            var payoransw="回答 "+d.answerCount;
            if(d.ExpId&&d.ExpId!=0){
                tohrefurl="http://www.pulu.com/weixin/shareexppage.html?eid="+d.ExpId+"#fromuser="+id;
                tranword="(悬赏完成转经验)";
                overword="";//结帖清除
                payoransw="付费 "+d.payCount;
                money="<span class='havemy xs'> 悬赏 ¥ "+score+"</span>";
            }
            //昵称
            var unick="";
            if(d.questionUserNick){
                //是不是推荐
                var tuijianword="";
                var moneysty="";
                if(d.isstanger===1){
                    if(d.ishidename===1){unick="<a>系统推荐 匿名 的"+typename+"</a>";}
                    else{unick="<a href='otherpeoplepage.html?otherid="+d.questionUserId+"'>系统推荐 <i class='usernick'>"+d.questionUserNick+"</i> 的悬赏</a>";}
                }
                else{
                    if(d.ishidename===1){unick="<a>匿名</a>";}
                    else{unick="<a href='otherpeoplepage.html?otherid="+d.questionUserId+"'><i class='usernick'>"+d.questionUserNick+"</i></a>";}
                }
            }
            //地址
            var location="";
            if(d.pululocation){
                location="<div style='position:relative;margin-bottom:6px;' onclick='hrefgo(this)' data-page='#mainbody' data-href='"+tohrefurl+"'><img src='img/dingwei.png' style='height:12px;position: absolute;bottom:3px;left:0;'/><span style='color:#999;padding-left:15px;font-size:12px;'>"+d.pululocation+"</span></div>"
            };
            //关键词
            var keywordnor;
            var keyword="<span class='keywordtit'>关于：</span>";
            if(d.keywords){
                keywordnor=d.keywords.split(",");
                for(var a=0;a<keywordnor.length;a++){
                    keyword+="<span>"+keywordnor[a]+"</span>";
                }
            }
            //我也说说
            var gotoheiexp="<div class='gotoheiexp' onclick='gotoheiexp(this)' data-qtype='2' data-keyword="+d.qid+">这些关键词,我也来说说</div>";
            var tit=codetoface(d.title);
            var fromnick="悬赏";//来自
            var des="";
            if(!d.shareUserId){if(!tim||/[a-z]/.test(tim)){tim="刚刚";}}
            else if(d.shareUserId&&d.shareUserId.length>0){
                unick="<a href='otherpeoplepage.html?otherid="+d.shareUserId+"'><i class='usernick'>"+d.shareUserNick+"</i> 的转发</a>";
                if(d.shareUserAva){
                    uimgword="<a href='otherpeoplepage.html?otherid="+d.shareUserId+"'><img class='profile_photo_img' src='http://42.62.66.198:8003/"+d.shareUserAva+"' onload='showloadimg(this)' style='display:none;'></a>";
                }else{
                    uimgword="<a href='otherpeoplepage.html?otherid="+d.shareUserId+"'><img onclick='gohrefftn(this)' class='profile_photo_img' src='../img/af8.png' onload='showloadimg(this)' style='display:none;'></a>";
                };
                if(d.des){des=codetoface(d.des);}
                var sharetim="";
                if(d.shareTime){sharetim=getDateDiff(getDateTimeStamp(d.shareTime));}
                //转发调整金额样式
                if(d.status.toString()=="0"){
                    overword="(已结帖)";
                    money="<span class='havemy xs' "+moneysty+"> ¥ "+score+"已分配</span>";
                }
                else{money="<span class='havemy xs' "+moneysty+"> 悬赏 ¥ "+score+"</span>";}
                if(!sharetim||/[a-z]/.test(sharetim)){sharetim="刚刚"};
            }
            tim='<div class="ContentFooter">'+tim+'</div>';
            $("#mainbody .addlistplace").append("<div class='pagedlist_item' onclick='hrefgo(this)' data-shareuserid='"+d.shareUserId+"' data-page='#mainbody' data-href='"+tohrefurl+"'><div class='feed_item feed_type_question'><div class='borderbox' data-shareuserid='"+d.shareUserId+"' data-page='#mainbody' data-href='http://www.pulu.com/weixin/questiondetail.html?qid="+d.qid+"#fromuser="+id+"'>"+"<div class='ContentHeader'><div class='photo_info_layout size_small'><div class='photo_wrapper'><div class='photo'>"+uimgword+"</div></div><div class='info_wrapper'><span class='feed_item_answer_user'> <span class='user'>"+unick+"</span>"+money+"</span>"+tim+"</div></div></div>"+"<div class='info_wrapper' style='margin-bottom:7px;'> <div class='info'> <span class='feed_item_answer_user'>"+des+"</span></div> </div> <h2><a class='question_link' onclick='hrefgo(this)' data-shareuserid='"+d.shareUserId+"' data-page='#mainbody' data-href='"+tohrefurl+"'>"+overword+tranword+tit+"</a></h2>"+showimgul+"<div class='ActionBar Question'>"+location+ "<div class='question_topics_container' onclick='gotoheiexp(this)' data-qtype='2' data-keyword="+d.qid+" >"+"<div class='QuestionTopics has_topics'>"+keyword+gotoheiexp+"</div></div><div class='action_bar_inner'><div class='action_item'> <a class='ReadMoreActionItem'>"+count+" 浏览 · "+ d.answerCount+" 回答 · "+d.webchatCount+" 转发到微信</a</div></div></div></div></div></div>")
        };
    };

/*
 新闻加载方法
 */
    //跳转 历史定位
    function newsgohref(e){
        console.log("保存的地址："+e.dataset.id);
        if(e.dataset.id){
            var neid=e.dataset.id;
            $.ajax({
                type:"post",
                datatype:"json",
                url:addcounturl,
                data:{"newsId":neid},
                success:function(d){
                    savepage();//保存页码 距离
                    location.href="http://www.pulu.com/weixin/newsdetail.html?newsid="+neid+"#fromuser="+id+"#newsShareId="+e.dataset.shareid+"#homepage=1";
                }
            })
        }
    }
    //跳转到新闻列表页面
    function gonewstype(e){
        var newstypeid= e.dataset.ntype;
        $.cookie("newstypeid",newstypeid);
        savepage();//保存页码 距离
        location.href="http://www.pulu.com/weixin/mycountry.html";
    }
    //获取数据
    function news(d){
        //用户有头像
        var uimgword="";
        if(d.shareUserAva){
            uimgword="<img onclick='usergohref(this)' data-uid='"+d.shareUserId+"' class='profile_photo_img' src='http://42.62.66.198:8003/"+d.shareUserAva+"' onload='showloadimg(this)' style='display:none;'>";
        }else{
            uimgword="<img onclick='usergohref(this)' data-uid='"+d.shareUserId+"' class='profile_photo_img' src='../img/af8.png' onload='showloadimg(this)' style='display:none;'>";}
        // 用户有昵称
        var unick="";
        if((d.shareUserNick&&d.shareUserNick.length>0)||d.typeid==202){unick="<i><i class='usernick' onclick='usergohref(this)' data-uid='"+d.shareUserId+"'>"+d.shareUserNick+"</i></i>";}
        //用户转发新闻有自己的评论
        var uview="";
        if(d.des){
            var newcontentcode=codetoface(d.des);//文字中插入表情图片
            uview="<div class='info_wrapper senderview'><div class='info'><span class='feed_item_answer_user'>"+newcontentcode+"</span></div></div>"
        }
        //时间
        var datatime="";
        if(d.shareTime){datatime=getDateDiff(getDateTimeStamp(d.shareTime));}
        if(!datatime||datatime=="undefined"){datatime="刚刚"};
        //新闻是否有评论  是否有点赞
        var ncom="";
        var haveheartname= d.upCount+" 赞";//是否点赞过 修改名称
        //系统推荐
        var disp="";//display
        var palf="";//padding-left
        var tuijianword="来自 ";
        if(d.istopnews===1){
            unick="<i onclick='gonewstype(this)' data-ntype='"+d.newsTypeId+"'>订阅号- <b>"+d.newsType+"</b></i>";
            uimgword="<img class='profile_photo_img' src='hyimg/"+d.newsTypeId+".png' onload='showloadimg(this)' style='display:none;'>";
        }
        var typeneme=d.newsType+"·",sitename=d.siteName+"·";
        if(d.newsTypeId==202){typeneme="";sitename=""};//转发网址 外部
        //新闻有图片
        var newsimgurl="img/puluweixin.jpg";
        if(d.newsImg){newsimgurl=d.newsImg;}
        var imgword="<div class='feed_answer_preview answer_content'><h2 class='no_highlight'><a class='question_link hrefto' onclick='newsgohref(this)' data-id='"+d.qid+"' data-title='"+d.title+"' data-shareid='"+d.shareId+"'><span class='question_text'><span class='rendered_qtext'>"+d.title+"</span></span></a></h2><a onclick='newsgohref(this)' data-id='"+d.qid+"' data-title='"+d.title+"' data-shareid='"+d.shareId+"'><div class='truncated_newsImgnail_holder'><span class='rendered_qtext'><div class='qtext_image_wrapper'><canvas class='qtext_image_placeholder landscape qtext_image zoomable_in_feed' width='500' height='500' style='visibility: hidden;'></canvas><img class='landscape qtext_image zoomable_in_feed lazy_loaded lazy_loading' src='"+newsimgurl+"' onload='showloadimg(this)' style='display:none;'></div></span></div></a></div>";
        $("#mainbody .addlistplace").append("<div class='pagedlist_item'> <div class='feed_item feed_type_answer'>"+"<div class='ContentHeader'> <div class='photo_info_layout size_small'> <div class='photo_wrapper' "+disp+"> <div class='photo'> "+ uimgword+" </div> </div> <div class='info_wrapper' "+palf+"> <span class='feed_item_answer_user'> <span class='user'>"+unick+"</span><span class='sign'></span> </span><div class='ContentFooter' onclick='gonewstype(this)' data-ntype='"+d.newsTypeId+"'><b>"+typeneme+"</b>"+sitename+datatime+"</div></div> </div> </div> "+uview+"<div class='Answer'><div class='QuestionAnswerVerbose SimpleToggle Toggle'>"+imgword+"<div class='ActionBar'><div class='action_bar_inner' "+disp+">"+"<div class='actionlist' id='actionlist"+d.shareId+"'><div id='newsid"+d.qid+"' data-shareid='"+d.shareId+"' class='addnewscom'>"+d.count+" 浏览 · "+haveheartname+" · "+ d.webchatCount+" 转发到微信</div></div></div></div>"+ncom+"</div></div></div></div>");

    };

/*
 所有方法
 */
    //编写函数列队方法 next();
    var stack=[];//保存执行函数数组
    var messarrid=[];//保存信息 ↑stack中函数使用
    function next(){
        //执行最靠前的一个
        var fn=stack[0];
        if(typeof fn==="function"){
            console.log("next执行");
            fn(messarrid[0],15);
        }else{
            console.log("结束");
            var toTop= parseInt($.cookie("puluscrollTop"));
            $(window).scrollTop(toTop);
            //还原参数
            pageSize=15;
            currentIndex=parseInt($.cookie("pulucurrentIndex"));
            console.log("pageSize:"+pageSize+";currentIndex:"+currentIndex);
            //清除参数
            $.cookie("puluscrollTop","");
            $.cookie("pulucurrentIndex","");
        };
    }
    //获取所有数据
    function getallnote(currentIndex,pageSize){
        console.log("所有数据执行");
        console.log(stack);
        console.log(messarrid);
        readyover=1;
        if($.cookie("pululot")&&$.cookie("pulutype")==3){
            var urllot="&lot="+$.cookie("pululot");
            var urllat="&lat="+$.cookie("pululat");
        }else{
            var urllot="";
            var urllat="";
        }
        $.ajax({
            type:"get",
            url:puluindexlisturl+ $.cookie("MYID")+"&currentIndex="+currentIndex+"&pageSize="+pageSize+"&type="+ $.cookie("pulutype")+urllot+urllat,
            success:function(d){
                if(d.Message=="没有数据"||!d.Data||d.Data.length==0){
                    readyover=1;
                    $("#waiting1").css("display","none");
                    $("#nonews1").css("display","block");
                }
                else{
                    for(var i=0;i< d.Data.length;i++){
                        //新闻
                        if(d.Data[i].qtype===1){news(d.Data[i]);}
                        //问题
                        else if(d.Data[i].qtype===2){que(d.Data[i]);}
                        //经验
                        else if(d.Data[i].qtype===3){jy(d.Data[i]);}
                    }
                    //页面定位
                    if($.cookie("pulucurrentIndex")&&/[0-9]/.test($.cookie("pulucurrentIndex"))){
                        if(/^[0-9]+$/.test($.cookie("puluscrollTop"))){
                            console.log("执行下一步");
                            stack.shift();
                            messarrid.shift();
                            next();
                        }
                    }
                    readyover=0;
                }
            }
        })
    }


function loadpage(){
    if($.cookie("pulutype")){
        $("#typelist li").attr("class","typelist");
        switch($.cookie("pulutype")){
            case "1":
                $("#labeltypename i").html("时间");
                $("#sjType").attr("class","typelist active");
                break;
            case "3":
                $("#labeltypename i").html("附近");
                $("#fjType").attr("class","typelist active");
                break;
            case "2":
                $("#labeltypename i").html("浏览量");
                $("#llType").attr("class","typelist active");
                break;
            case "4":
                $("#labeltypename i").html("转发量");
                $("#zfType").attr("class","typelist active");
                break;
        }
    }

    if($.cookie("pulucurrentIndex")&&/[0-9]/.test($.cookie("pulucurrentIndex"))){
        $("#mainbody div.addlistplace div").remove();//清空页面
        if(/^[0-9]+$/.test($.cookie("puluscrollTop"))){
            pageSize=parseFloat($.cookie("pulucurrentIndex"))*pageSize;
            currentIndex=1;//一页加载全部
        }else{
            pageSize=15;
            currentIndex=1;
        }
        //loadtime=parseFloat($.cookie("pulucurrentIndex"));
        for(var i=0;i<parseFloat($.cookie("pulucurrentIndex"));i++){
            stack.push(getallnote);
            messarrid.push(i+1);
        }
        console.log($.cookie("pulucurrentIndex"));

        next();
        //getallnote(currentIndex,pageSize);
    }
    else{
        id=$.cookie("MYID");
        getallnote(currentIndex,pageSize);
    }
};
$(window).scroll(function(){
    $("#typelist").hide();
    $(".quicklist").css("display","none");
    $("#refreshbtn").css("display","block");
    var mainheight=$("#page_wrapper").height();
    var windowheight=$(window).height();
    var chajuli=$(window).scrollTop()-$("#page_wrapper").offset().top;
    var lastheight=mainheight-50;//极限距离
    //console.log($(window).scrollTop());
    if((chajuli+windowheight)>lastheight&&readyover===0){
        currentIndex++;
        console.log("调用:currentIndex "+currentIndex);
        readyover=1;
        getallnote(currentIndex,pageSize);
    }
});

/*
 切换类型重新加载
 */
$("#choicetype").click(function(){
    $("#typelist").toggle();
});
$("#typelist li.typelist").click(function(i){
    //取消事件冒泡
    var e=arguments.callee.caller.arguments[0]||event; //若省略此句，下面的e改为event，IE运行可以，但是其他浏览器就不兼容
    if (e && e.stopPropagation) {
        // this code is for Mozilla and Opera
        e.stopPropagation();
    } else if (window.event) {
        // this code is for IE
        window.event.cancelBubble = true;
    }
    var thisid=i.target.id;
    $("#typelist li").attr("class","typelist");
    $("#"+thisid).attr("class","typelist active");
    $("#labeltypename i").html(i.target.dataset.name);
    $("#typelist").hide();
    $("#typelist").css("display","none");
    //数据还原
    $("#waiting1").css("display","block");
    $("#nonews1").css("display","none");
    $("#nonews").css("display","none");
    pageSize=15;
    currentIndex=1;
    //页面重新加载
    $("#mainbody div.addlistplace div").remove();//清空页面
    switch(thisid){
        case "sjType":
            $.cookie("pulutype",1);
            getallnote(currentIndex,pageSize);
            break;
        case "fjType":
            $.cookie("pulutype",3);
            mapftn();
            break;
        case "llType":
            $.cookie("pulutype",2);
            getallnote(currentIndex,pageSize);
            break;
        case "zfType":
            $.cookie("pulutype",4);
            getallnote(currentIndex,pageSize);
            break;
    }
});

var returnurl=encodeURIComponent("http://www.pulu.com/weixin/pulu.html"); //同意后跳转页面
$(document).ready(function(){
    if(is_weixn()){
        if($.cookie("MYID")){ //$.cookie("wxopenid")
            loadpage();
        }
        else{
            //先微信验证获取openid 如果已经授权过了 直接页面截取openid 然后发送请求获取 用户的ID
            var reg=new RegExp("code=[a-zA-Z0-9]*");
            if(window.location.href.match(reg)){
                var code=window.location.href.match(reg);
                $.ajax({
                    type:"GET",
                    url:"GetOpenId.ashx?"+code,
                    async: false,
                    success:function(data){
                        console.log(data);
                        var dat=data;
                        $.cookie("wxod",dat);
                        $.cookie("wxopenid",dat);
                        //发请求获取用户的id
                        $.ajax({
                            type:"GET",
                            url:getwxopenidurl+dat,
                            success:function(d){
                                //如果没有用户id 则证明用户是第一次登陆 保存openid跳转至登录页面
                                if(!d.Data||!d.Data.userId){
                                    //微信跳转进来的
                                    if(!$.cookie("MYID")||$.cookie("MYID").length<10){
                                        $.cookie("wxopenid",dat);
                                        location.href="http://www.pulu.com/weixin/login.html"
                                    }
                                    //退出登录的
                                }else if(d.Data&&d.Data.userId.length>0){
                                    $.cookie("wxopenid",d.Data.openId);
                                    $.cookie("MYID",d.Data.userId);
                                    $.cookie("MYAVA",d.Data.userAva);
                                    $.cookie("MYNICK",d.Data.userNick);
                                    //id=d.Data.userId;
                                    //location.href="http://www.pulu.com/weixin/pulu.html"
                                    loadpage();
                                }
                            }
                        })
                    }
                })
            }
            else{
                location.href="https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx658c34793ecfd9f9&redirect_uri="+returnurl+"&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect";
            }
        }
    }
    else{
        if(!$.cookie("MYID")){location.href="http://www.pulu.com/weixin/login.html"}
        else{
            if(!/https/.test(location.href)){
                location.href="https://www.pulu.com/weixin/pulu.html";
            }
            loadpage();
        }
    }
});